/******************************************************************************
* Copynext (c) 2007 Alexandru Scvortov
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the nexts
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copynext notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYnext HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/


#include "common.h"
#include <string.h>

static VarNode* var_node_new() {
    VarNode *tmp = (VarNode*)malloc(sizeof(VarNode));

    tmp->child = 0;
    tmp->next = 0;
    tmp->c_node = 0;
    tmp->name = 0;
	tmp->file_name = 0;
	tmp->line_num = 0;

    return tmp;
}

VarNode* var_node_remove(VarNode *vn) {
    if (!vn)
        return 0;

    WordNode *it;
    it = vn->child;
    while (it) {
        WordNode *next = it->next;
        word_node_remove(it);
        it = next;
    }

    check_and_free(vn);

    return vn;
}

static WordNode* split_var(char *text) {
    char *n = strchr(text, ' ');

    char *aux = strchr(text, '*');
    if ((aux && (aux < n)) || !n)
        n = aux;

    WordNode *tmp = word_node_new();
    if (!n) {
        tmp->value = (char*)malloc((strlen(text) + 1) * sizeof(char));
        strcpy(tmp->value, text);
        tmp->type = WordNodeName;
    } else {
        *n = 0;
        tmp->value = (char*)malloc((strlen(text) + 2) * sizeof(char));
        if (n == aux) {
            tmp->value[0] = '*';
            tmp->value[1] = 0;
            strcat(tmp->value, text);
        } else
            strcpy(tmp->value, text);
        tmp->next = split_var(n + 1);
    }

    return tmp;
}

void parse_var() {
    VarNode *vn = var_node_new();
    vn->child = split_var(current_value);

    WordNode *it = vn->child;
    while (it) {
        if (WordNodeName == it->type)
            vn->name = it;
        NEXT(it);
    }

    if (comments && !comments->assigned) {
        vn->c_node = comments;
        comments->assigned = 1;
    } else {
        fprintf(stderr, "W: Declaration of variable %s has no comment.\n", vn->name->value);
    }

	vn->file_name = current_file;
	vn->line_num = cur_line_num;

    vn->next = variables;
    variables = vn;
}
